/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (C) 2014 RayBow and/or its affiliates. All rights reserved.
 */
package top.swimmer.core.mybatis.convert;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.postgresql.util.PGobject;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * UserTypeHandler.
 */
public class PGObjectHandler extends BaseTypeHandler<PGobject> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, PGobject parameter, JdbcType jdbcType) throws SQLException {
        ps.setObject(i, parameter);
    }

    @Override
    public PGobject getNullableResult(ResultSet rs, String columnName) throws SQLException {
        PGobject i = (PGobject) rs.getObject(columnName);
        return rs.wasNull() ? null : i;
    }

    @Override
    public PGobject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        PGobject i = (PGobject) rs.getObject(columnIndex);
        return rs.wasNull() ? null : i;
    }

    @Override
    public PGobject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        PGobject i = (PGobject) cs.getObject(columnIndex);
        return cs.wasNull() ? null : i;
    }
}
